package com.lzj.demo.controller;

import com.lzj.common.annotation.Log;
import com.lzj.common.annotation.RepeatSubmit;
import com.lzj.common.core.controller.BaseController;
import com.lzj.common.core.domain.PageQuery;
import com.lzj.common.core.domain.R;
import com.lzj.common.core.page.TableDataInfo;
import com.lzj.common.core.validate.AddGroup;
import com.lzj.common.core.validate.EditGroup;
import com.lzj.common.enums.BusinessType;
import com.lzj.common.utils.poi.ExcelUtil;
import com.lzj.demo.domain.bo.TestTableBo;
import com.lzj.demo.domain.vo.TestTableVo;
import com.lzj.demo.service.ITestTableService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 测试测试
 *
 * @author lzj
 * @date 2024-08-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/table")
public class TestTableController extends BaseController {

    private final ITestTableService iTestTableService;

    /**
     * 查询测试测试列表
     */
    @GetMapping("/list")
    public TableDataInfo<TestTableVo> list(TestTableBo bo, PageQuery pageQuery) {
        return iTestTableService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出测试测试列表
     */
    @Log(title = "测试测试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TestTableBo bo, HttpServletResponse response) {
        List<TestTableVo> list = iTestTableService.queryList(bo);
        ExcelUtil.exportExcel(list, "测试测试", TestTableVo.class, response);
    }

    /**
     * 获取测试测试详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public TestTableVo getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return iTestTableService.queryById(id);
    }

    /**
     * 新增测试测试
     */
    @Log(title = "测试测试", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TestTableBo bo) {
        return toAjax(iTestTableService.insertByBo(bo));
    }

    /**
     * 修改测试测试
     */
    @Log(title = "测试测试", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TestTableBo bo) {
        return toAjax(iTestTableService.updateByBo(bo));
    }

    /**
     * 删除测试测试
     *
     * @param ids 主键串
     */
    @Log(title = "测试测试", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTestTableService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
